package com.km.noh.weatherinfo.util;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;

import com.km.noh.weatherinfo.R;
import com.km.noh.weatherinfo.WeatherInfoActivity;
import com.km.noh.weatherinfo.async.WeatherDataAsync;

public class KMDialog {
    
	private static AlertDialog dialog;
	private static ProgressDialog progressDialog = null;
	
    private static String getTitle(Context context) {
    	return context.getString(android.R.string.dialog_alert_title);
    }
    
    /** 
     * Dialog Alert
     */
    public static boolean showAlert(Context context, String message) {    	    	
    	return showAlert(context, KMDialog.getTitle(context) , message,false);
    }    
    /**
     * Dialog Alert
     */
    public static boolean showAlert(Context context, int message) {
    	return showAlert(context,android.R.string.dialog_alert_title , message, false);
    }    
    /**
     * Dialog Alert
     */
    public static boolean showAlert(Context context, String message , final boolean isFinish) {
    	return showAlert(context,KMDialog.getTitle(context) , message,isFinish);
    }
    /**
     * Dialog Alert
     */
    public static boolean showAlert(Context context, int message , final boolean isFinish) {
    	return showAlert(context,android.R.string.dialog_alert_title , message,isFinish);
    }
    /**
     * Dialog Alert
     */
    public static boolean showAlert(Context context,int title, int message) {
    	return showAlert(context,title, message ,false);
    }
    /**
     * Dialog Alert
     */
    public static boolean showAlert(Context context,String title, String message) {
    	return showAlert(context,title , message,false);
    }
    /**
     * Dialog Alert 
     */
    public static boolean showAlert(Context context,int title, int message ,final boolean isFinish) {
        return showAlert(context, context.getString(title) , context.getString(message), isFinish);
    }    
    /**
     * Dialog Alert
     */
    public static boolean showAlert(final Context context,String title, String message, final boolean isFinish) {
        try{
        	closeProgress(context);
        	closeDialog(context);
	        dialog =new AlertDialog.Builder(context)
	        .setTitle(title)
	        .setMessage(message)
	        .setIcon(android.R.drawable.ic_dialog_alert)
	        .setCancelable(false)
	        .setPositiveButton(android.R.string.ok,
	                new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int which) {
	                        dialog.dismiss();
	                    }
	                }).show();
        }catch (Exception e) {
        	Log.e("", e.toString(), e);
		}
        return true;
    }
    
    public static boolean showSelectCity(final WeatherInfoActivity mActivity){
    	final String[] cityList = mActivity.getResources().getStringArray(R.array.city_name_array);
    	final String[] cityListKo = mActivity.getResources().getStringArray(R.array.city_name_array_ko);
    	try{
    		closeProgress(mActivity);
    		closeDialog(mActivity);
    		
    		AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
    		builder.setTitle(R.string.city_select_popup_title);
        	builder.setSingleChoiceItems(cityListKo, 0, new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					KMPreference.getInstance(mActivity).setData(KMPreference.SELECTED_CITY, cityList[which]);
					KMPreference.getInstance(mActivity).setData(KMPreference.SELECTED_CITY_KO, cityListKo[which]);
					new WeatherDataAsync(mActivity).execute();
					dialog.dismiss();
				}
			})
        	.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});  	
			dialog = builder.show();
			
    	}catch (Exception e) {
    		Log.e("", e.toString(), e);
    	}
    	
    	return true;
    }
    
	
    public static void showProgress(Context context){
    	try{
    		if(progressDialog!=null){
    			progressDialog.cancel();
    			progressDialog.dismiss();
    		}
    			
    		//else
    		progressDialog = new ProgressDialog(context);
    		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    		progressDialog.setMessage("Loding...");
    		progressDialog.show();
    	}catch (Exception e) {
    		Log.e("", e.toString(), e);
		}
	}
    
    public static void closeDialog(Context context){
		try {
			if (dialog != null){ 
				dialog.dismiss();
			}
			dialog = null;
		} catch (Exception e) {
			Log.e("", e.toString(), e);
		}

    }
    
    public static AlertDialog getDialog() {
    	return dialog;
    }
    
    public static void closeProgress(Context context){
		try {
	    	if(progressDialog!=null){
	    		progressDialog.dismiss();
	    	}
	    	progressDialog = null;
		} catch (Exception e) {
			Log.e("", e.toString(), e);
		}
    }
    
	private static void cleanUp(){
//		BookshelfActivity.cleanUp();	//bookshelf clean up ;
	}
}
